<!--
 * @Author: wangjianwhy wangjianwhy@sina.com
 * @Date: 2024-10-23 16:24:09
 * @LastEditors: wangjianwhy wangjianwhy@sina.com
 * @LastEditTime: 2024-10-25 10:23:48
 * @FilePath: \my-vue3-appg:\vscode-work\tlo-shopping\src\views\mall\SearchShopping.vue
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
-->
<template>
    <div class="search-global">
        <!--头部搜索框-->
        <div class="search-title">
            <div class="title-fixed">
                <span class="title-top">
                    <img src="/public/images/search.svg" />
                    <span class="close" @click="cleanSearch" v-if="searchText">×</span>
                    <input type="text" placeholder="智能手表" ref="searchContent" v-model="searchText" />
                    <label @click="router.back">取消</label>
                </span>
            </div>
        </div>

        <!--热门搜索，最多显示10个-->
        <div class="search-hot">
            <p>热门搜索</p>
            <div class="hot-tag">
                <span @click="ProductDetail(tag.id)" v-for="(tag, index) in hotTags" :key="index">{{ tag.hotKeyWord }}</span>
            </div>
        </div>

        <!--历史搜索，最多显示10个-->
        <div class="search-histroy">
            <p>
                <label>历史搜索</label>
                <img src="/src/assets/icons/svg/delete.svg" @click="cleanHis" />
            </p>
            <div v-if="histroySearch" class="hot-tag">
                <span v-for="(his, index) in histroySearch" :key="index" @click="searchHis(his.message)">{{his.message}}</span>
            </div>
            <div v-else class="hot-tag">
                <label style="font-size: 14px; color: #666666;">暂无搜索记录</label>
            </div>
        </div>

        <!--热搜榜 top:10-->
        <div class="hot-top">
            <p class="hot-title">🔥热搜榜</p>
            <div class="top-10" v-for="(hot, index) in hotLists" :key="index" @click="productDetail(hot.id)">
                <div :class="{'top-no': true, 'top-no1': hot.index === 1, 'top-no2': hot.index === 2, 'top-no3': hot.index === 3}">{{ hot.index }}</div>
                <div class="top-img"><img :src="hot.imgurl" /></div>
                <div class="top-name">
                    <div>{{ truncateText(hot.title, 18) }} </div>
                    <div style="font-size: 13px; color: #666666;">人气值 {{ hot.likeValue }}</div>
                </div>
            </div>
        </div>

    </div>
<MessageConfirm :show="isConfirmVisible" :message="confirmMessage" @update:show="isConfirmVisible = $event" @confirm="handleConfirm" @cancel="handleCancel" />
</template>

<script setup>
import { useRouter } from 'vue-router';
import { ref } from 'vue';
import MessageConfirm from '@/components/MessageConfirm.vue';
import { truncateText } from '@/components/util/tools.js';

const router = useRouter();

const hotTags = ref([
    {
        id: 1,
        hotKeyWord: '热门关键词1'
    },{
        id: 2,
        hotKeyWord: '热门关键词2'
    },{
        id: 3,
        hotKeyWord: '热门关键词3'
    },{
        id: 4,
        hotKeyWord: '热门关键词4'
    },{
        id: 5,
        hotKeyWord: '热门关键词5'
    },{
        id: 6,
        hotKeyWord: '热门关键词6'
    }
]);

const histroySearch = ref([
    {
        id: 1,
        message: '乐享厨房套餐1'
    },{
        id: 2,
        message: '乐享厨房套餐2'
    },{
        id: 3,
        message: '乐享厨房套餐3'
    },{
        id: 4,
        message: '乐享厨房套餐4'
    },{
        id: 5,
        message: '乐享厨房套餐5'
    },{
        id: 6,
        message: '乐享厨房套餐6'
    },{
        id: 7,
        message: '乐享厨房套餐7'
    },{
        id: 8,
        message: '乐享厨房套餐8'
    },{
        id: 9,
        message: '乐享厨房套餐9'
    },{
        id: 10,
        message: '乐享厨房套餐10'
    }
]);

const searchContent = ref('');
const searchText = ref('');

// 清理搜索输入框的内容
const cleanSearch = () => {
    if (searchContent.value) {
        searchContent.value = '';
        searchText.value = '';
    }
}

// 清理历史搜索的信息
const cleanHis = () => {
    isConfirmVisible.value = true;
}

const ProductDetail = (id) => {
    router.push({ name: 'productDetail', query: { id: id }});
}

const isConfirmVisible = ref(false);
const confirmMessage = ref('您确定要删除吗？');

const handleConfirm = () => {
    alert("确认的逻辑处理");
  // 处理确认逻辑，发起请求删除历史搜索的记录
  histroySearch.value = null;
};

const handleCancel = () => {
  // 处理取消逻辑，什么都不做
};

//热搜榜
const productDetail = () => {

}

const searchHis = (message) => {
    router.push({ name: 'queryShopList', query: { searchMessage: message }});
}

const hotLists = ref([
    {
        id: 1,
        imgurl: '/src/assets/lunbo/image3.jpeg',
        title: 'Haier/海尔 电热水器 ES5000-GM1-GFDXX3000dd',
        likeValue: 10292,
        index: 1
    },{
        id: 2,
        imgurl: '/src/assets/lunbo/image3.jpeg',
        title: 'Haier/海尔 电热水器 ES5000-GM1-GFDXX3000dd',
        likeValue: 10292,
        index: 2
    },{
        id: 3,
        imgurl: '/src/assets/lunbo/image3.jpeg',
        title: 'Haier/海尔 电热水器 ES5000-GM1-GFDXX3000dd',
        likeValue: 10292,
        index: 3
    },{
        id: 4,
        imgurl: '/src/assets/lunbo/image3.jpeg',
        title: 'Haier/海尔 电热水器 ES5000-GM1-GFDXX3000dd',
        likeValue: 10292,
        index: 4
    },{
        id: 5,
        imgurl: '/src/assets/lunbo/image3.jpeg',
        title: 'Haier/海尔 电热水器 ES5000-GM1-GFDXX3000dd',
        likeValue: 10292,
        index: 5
    },{
        id: 6,
        imgurl: '/src/assets/lunbo/image3.jpeg',
        title: 'Haier/海尔 电热水器 ES5000-GM1-GFDXX3000dd',
        likeValue: 10292,
        index: 6
    },{
        id: 7,
        imgurl: '/src/assets/lunbo/image3.jpeg',
        title: 'Haier/海尔 电热水器 ES5000-GM1-GFDXX3000dd',
        likeValue: 10292,
        index: 7
    },{
        id: 8,
        imgurl: '/src/assets/lunbo/image3.jpeg',
        title: 'Haier/海尔 电热水器 ES5000-GM1-GFDXX3000dd',
        likeValue: 10292,
        index: 8
    },{
        id: 9,
        imgurl: '/src/assets/lunbo/image3.jpeg',
        title: 'Haier/海尔 电热水器 ES5000-GM1-GFDXX3000dd',
        likeValue: 10292,
        index: 9
    },{
        id: 10,
        imgurl: '/src/assets/lunbo/image3.jpeg',
        title: 'Haier/海尔 电热水器 ES5000-GM1-GFDXX3000dd',
        likeValue: 10292,
        index: 10
    }
]);

</script>

<style scoped>
.search-global {
    background-color: #fff;
}

/*头部搜索*/
.search-title {
    width: 100%;
    height: 35px;
    position: fixed;
    top: 0;
    padding: 10px 0px;
    z-index: 100;
    background-color: #fff;
}
.title-fixed {
    width: 90%;
    margin: auto;
    height: 35px;
}
.title-top img {
    width: 20px;
    height: 20px;
    float: left;
    position: absolute;
    left: 6%;
    margin-top: 6px;
}
.title-top input {
    height: 30px;
    border-radius: 50px;
    width: 80%;
    margin-left: 0px;
    float: left;
    padding-left: 25px;
    background-color: #ece9e9;
    border: 0px;
}
.title-top label {
    font-size: 18px;
    width: auto;
    float: right;
    text-align: right;
    padding-top: 2px;
}
.title-top .close {
    width: 18px;
    height: 18px;
    position: absolute;
    left: 78%;
    cursor: pointer;
    border-radius: 50px;
    background-color: #908c8c;
    margin: auto;
    font-size: 14px;
    color: #ffffff;
    margin-top: 6px;
}

/*热门搜索*/
.search-hot {
    margin: auto;
    margin-top: 45px;
    width: 90%;
    overflow: hidden;
}
.search-hot p {
    text-align: left;
    font-weight: bold;
}
.hot-tag {
    height: 35px;
    width: 100%;
    overflow-x: auto; /* 启用水平滚动 */
    white-space: nowrap; /* 防止内容换行 */
    scrollbar-width: none; /* Firefox 隐藏滚动条 */
    text-align: left;
}
.hot-tag span {
    width: auto;
    background-color: #cccccc;
    font-size: 13px;
    border-radius: 50px;
    padding: 3px 8px 1px 8px;
    margin: 5px;
    display: inline-block;
    cursor: pointer;
}

/*历史搜索*/
.search-histroy {
    width: 90%;
    margin: auto;
}
.search-histroy p {
    width: 100%;
    height: 25px;
}
.search-histroy p label{
    text-align: left;
    font-weight: bold;
    float: left;
}
.search-histroy p img {
    float: right;
    height: 20px;
    width: 20px;
    cursor: pointer;
}

/*热搜榜*/
.hot-top {
    background-color: #FAEBD7;
    width: 90%;
    height: auto;
    margin: auto;
    margin-top: 20px;
    border-radius: 12px;
    padding: 0px 5px;
    text-align: left;
}
.hot-top p {
    padding-top: 5px;
    font-weight: bold;
    font-size: 18px;
    color: #D2691E;
}
.hot-top .top-10 {
    width: 100%;
    height: 50px;
    display: flex;
    justify-content: center; /* 水平居中 */
    align-items: center; /* 垂直居中 */
    margin: 10px 0px;
}
.top-10 .top-no {
    float: left;
    padding: 10px;
    font-size: 18px;
    height: 30px;
    width: 20px;
    text-align: left;
}
.top-no1 {
    font-weight: bold;
    color: #D2691E;
}
.top-no2 {
    font-weight: bold;
    color: #f1c232;
}
.top-no3 {
    font-weight: bold;
    color: #ffd966;
}
.top-10 img {
    width: 50px;
    height: 50px;
    border-radius: 8px;
}
.top-img {
    width: auto;
    float: left;
    height: 50px;
}
.top-name {
    height: 50px;
    font-size: 14px;
    width: auto;
    float: left;
    padding-left: 10px;
}
.top-name div {
    height: 25px;
    float: left;
    width: 100%;
    
}
</style>